package com.sir.tao.carhire.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sir.tao.carhire.common.CommonResult;
import com.sir.tao.carhire.entity.table.CarTypeEntity;
import com.sir.tao.carhire.service.CarTypeService;
import com.sir.tao.carhire.util.PageUtil;
import com.sir.tao.carhire.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author dyy
 * @data 2022/1/1 15:21
 * @description:
 */
@RestController
@RequestMapping("carType")
@Api(tags = "汽车类型管理接口")
public class CarTypeController {

    @Resource
    private CarTypeService carTypeService;

    /**
     * 获取所有的汽车类型
     * @return 汽车所有类型
     */
    @GetMapping("/list")
    @ApiOperation("获取所有的汽车类型")
    @ApiImplicitParam(name = "获取所有的汽车类型", value = "获取所有的汽车类型")
    public CommonResult<?> getAll(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  String search){
        List<CarTypeEntity> list = carTypeService.list(new LambdaQueryWrapper<CarTypeEntity>()
                .like(!StringUtils.isEmpty(search),CarTypeEntity::getCode,search)
                .or()
                .like(!StringUtils.isEmpty(search),CarTypeEntity::getName,search)
        );
        PageUtil page = PageUtil.doPage(pageNo,pageSize, list);
        return CommonResult.success(page);
    }

    /**
     * 新增商品类型
     * @param carTypeEntity 商品类型实体
     * @return 是否成功
     */
    @PostMapping("/add")
    @ApiOperation("新增商品类型")
    @ApiImplicitParam(name = "新增商品类型", value = "新增商品类型")
    public CommonResult<?> add(@RequestBody CarTypeEntity carTypeEntity){
        boolean i = carTypeService.save(carTypeEntity);
        if (i) {
            return CommonResult.success();
        } else {
            return CommonResult.fail("新增商品类型失败");
        }
    }

    /**
     * 删除一个汽车类型
     * @param carTypeId 删除一个汽车类型
     * @return 删除一个汽车类型
     */
    @DeleteMapping("/delete/{carTypeId}")
    @ApiOperation("删除一个汽车类型")
    @ApiImplicitParam(name = "删除一个汽车类型", value = "删除一个汽车类型")
    public CommonResult<?> delete(@PathVariable String carTypeId){
        boolean i = carTypeService.removeById(carTypeId);
        if (i) {
            return CommonResult.success();
        } else {
            return CommonResult.fail("删除失败");
        }
    }
}
